import * as Websocket from "ws";
import { VerifyClientCallbackSync } from "ws"
import { JwtWebSocket } from "../middleware/jwt";
const jwtVerify: VerifyClientCallbackSync = (info) => {

    // let token = info.req.headers.authorization
    let token = info.req.headers["sec-websocket-protocol"]
    // console.log("req", info.req.headers);

    if (JwtWebSocket(token)) {
        info.req.headers.userInfo = JwtWebSocket(token)
    }
    return JwtWebSocket(token)
}

//创建ws的服务端对象
const readyGameWS = new Websocket.Server({
    port: 8888,
    path:"/readyGame",
    // webId:
    // verifyClient: jwtVerify
})


// {
//     '加藤鹰': {
//       name: '加藤鹰',
//       player: { id: '', avatar: '', nickname: '' },
//       script: 1
//     },
//     '罗宾汉': {
//       name: '罗宾汉',
//       player: { id: '', avatar: '', nickname: '' },
//       script: 3
//     },
//     '陈国汉': {
//       name: '陈国汉',
//       player: { id: '', avatar: '', nickname: '' },
//       script: 4
//     }
//   }



// {
//     key: 'join',
//     role: {
//       name: '陈国汉',
//       player: { id: '', avatar: '', nickname: '' },
//       script: 4
//     },
//     player: {
//       userName: 'nxp001',
//       nickname: '牛小排',
//       role: 'boss',
//       token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyTmFtZSI6Im54cDAwMSIsInBhc3N3b3JkIjoiJDJhJDEwJHVmc2tUNTBXZ04uaERqNzZZS1QyNy5WMENCNUhIYUlNa2pyUlJQL2QvU0E4M3Rkam9BZlZ1IiwiaWQiOjEsIm5pY2tuYW1lIjoi54mb5bCP5o6SIiwicm9sZSI6ImJvc3MiLCJpc0RNIjp0cnVlLCJhdmF0YXIiOiJodHRwOi8vNDcuOTMuMjIxLjE1OC8wMDEv54mb5bCP5o6SLmpwZyIsImlhdCI6MTY0OTA3Mzg2OH0.RbpZRteQSRBXKbmEILTv0PThyZWENbIALutlYFuAqo4',
//       isDM: true,
//       avatar: 'http://47.93.221.158/001/牛小排.jpg'
//     },
//     roomId: '1649241797175.1.1'
//   }

export{
    readyGameWS
}

// ws.on("connection", (client, req) => {
//     // console.log("有客户端连接成功了", client)
//     //对客户端连接对象进行message事件监听
//     //msg: 由客户端发送给服务端的数据
//     console.log(client.url);
//     console.log("header", req.headers);

//     console.log("req", req.headers.userInfo);
//     const re = req
//     // client.send(re) 
//     client.send('你好')
//     client.on("message", msg => {
//         console.log("msg", msg.toString());
//         // console.log("msg2", msg.url);
//         client.send("尼玛")
//     })

// })


